package cn.bxshey.general.util;

import cn.bxshey.general.constant.JwtConstant;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @作者：不想说话而已
 * @Date：2023/12/2 11:47
 * <p>
 * IP 地址处理
 */

@Component
public class HttpUtils {

    private final Ip2regionSearcher ip2regionSearcher;

    public HttpUtils(Ip2regionSearcher ip2regionSearcher) {
        this.ip2regionSearcher = ip2regionSearcher;
    }

    /**
     * @return: Map：ip、ip地址、token令牌
     */
    public Map<String, String> getHttpInfo() {
        //获取request 对象
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();

        Map<String, String> info = new HashMap<>();
        Optional.of(request)
                .ifPresent(s -> {
                    //获取ip
                    String ipAddress = getIpAddress(request);
                    //使用工具获取ip归属地信息
                    IpInfo ipInfo = ip2regionSearcher.memorySearch(ipAddress);
                    //城市
                    info.put("city", ipInfo != null ? ipInfo.getAddress() : "未知");
                    //ip地址
                    info.put("ip", ipAddress);
                    //token
                    info.put(JwtConstant.ACCESS_TOKENS, request.getHeader(JwtConstant.ACCESS_TOKENS));
                });

        //返回ip信息
        return info.size() > 0 ? info : null;
    }

    //获取客户端ip
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 获取到多个ip时取第一个作为客户端真实ip
        if (StringUtils.isNotEmpty(ip) && ip.contains(",")) {
            String[] ipArray = ip.split(",");
            if (ArrayUtils.isNotEmpty(ipArray)) {
                ip = ipArray[0];
            }
        }
        return ip;
    }

    //获取response对象
    public HttpServletRequest getHttpRequest() {
        //获取request 对象
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

}
